import json
from card import Card
import codecs
import traceback

combo_multipliers = [
            [50, 1.],
            [100, 1.1],
            [200, 1.15],
            [400, 1.2],
            [600, 1.25],
            [800, 1.3],
            [3000, 1.35]
        ]
def get_combo_multiplier(combo):
    total = 0
    current = 0
    pos = 0
    while current < combo:
        notes = min(combo, combo_multipliers[pos][0])
        total += (notes - current) * combo_multipliers[pos][1]
        current = notes
        pos += 1
    return 1.0 * total / combo

def get_block(card_name, caption, font_size=10, idolized=True):
    result = '<div style="display:inline-block;margin-right:5px"><figure style="margin:0px;margin-top:2px"><img src="https://llsif2-1315296209.cos.ap-shanghai.myqcloud.com/card-avatar/'
    result += card_name + ("" if idolized else "未觉") + '.jpg" width="60" alt="'+ card_name +'"></figure><figcaption style="text-align:center;margin-bottom:5px;line-height:13px;font-size:12px">'+caption +'</figcaption></div>'
    return result

levels = {"SS": 8500, "S": 8000, "A": 7500,  "B": 7000, "C": 6000}
def generate_md(cards, color=None):
    card_levels = ["SS", "S", "A", "B", "C"]

    cards = sorted(cards, key=lambda x: x.strength[color], reverse=True)
    card_pos = 0
    result = ""

    result += "| 档位 | 强度 |\n"
    result += "| ---- | ---- |\n"
    for level in card_levels:
        result += "|" + level + "|"
        while card_pos < len(cards):
            card = cards[card_pos]
            if card.name in ["粉sr果", "蓝r果"]:
                card_pos += 1
                continue
            if card.strength[color] < levels[level]:
                break
            #if card.color == color or not color:
            caption = str(round(card.strength[color]))
            result += get_block(card.name, caption)
            card_pos += 1
        result += "|\n"
    return result

class Config():
    def __init__(self):
        self.combo = 750
        self.time = 110
        self.leader_bonus = 1.2 # 还要算助战leader
        self.color_bonus = 1.1
        self.group_bonus = 1.025
        self.perfect_rate = 0.95
        self.perfect_bonus = 0.88 + 0.12 * self.perfect_rate

        self.density_distribution = {
            0.5: 0.1,
            0.8: 0.2,
            1: 0.4,
            1.2: 0.2,
            1.5: 0.1,
        }

        self.combo_multiplier = get_combo_multiplier(self.combo)
        print("combo_multiplier", self.combo_multiplier)


if __name__ == "__main__":
    card = Card()
    config = Config()

    from openpyxl import load_workbook, Workbook
    workbook = load_workbook("card_data.xlsx")
    sheet = workbook["Sheet1"]
    
    cards = []
    for rownum in range(2, sheet.max_row + 1):
        try:
            card = Card()
            row = sheet[rownum]
            card.name = row[0].value
            card.color = row[1].value
            card.attribute["smile"] = int(row[2].value)
            card.attribute["pure"] = int(row[3].value)
            card.attribute["cool"] = int(row[4].value)

            card.skill["trigger"]["type"] = row[5].value
            card.skill["trigger"]["prob"] = row[6].value
            card.skill["trigger"]["number"] = row[7].value

            card.skill["effect"]["type"] = row[8].value
            if row[9].value:
                card.skill["effect"]["time"] = int(row[9].value)
            card.skill["effect"]["number"] = row[10].value

            for color in ["smile", "pure", "cool"]:
                card.calculate(color, config)
            print(card.name, card.strength)
            cards.append(card)
        except:
            print("total", rownum, "cards")
            traceback.print_exc()
            break
        
    result = ""
    result = "# 说明\n\n"
    result += "BiliBili：[Glaceon_LL研究部](https://space.bilibili.com/323681)\n\n"
    result += "1. 已考虑技能重叠触发\n\n"
    result += "2. 歌曲配置：750note，110秒，perfect率95%\n\n"
    for color in ["smile", "pure", "cool"]:
        result += "# " + color + "\n\n"
        result += "\n" + generate_md(cards, color=color)

    writefile = codecs.open("强度表.md", "w", "utf-8")
    writefile.write(result)
    writefile.close()
